package com.cms.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cms.common.kit.ResponseKit;
import com.cms.common.kit.StrKit;
import com.cms.common.util.pass.CryptUtils;
import com.cms.controller.BaseController;
import com.cms.model.Admin;
import com.cms.model.Role;
import com.cms.validate.Rule;
import com.cms.validate.ValidForm;


@Controller
@RequestMapping(value = "/admin/admin")
public class AdminController extends BaseController{

	private static final String LIST_PAGE="admin/admin/admin_list";
	private static final String ADD_PAGE="admin/admin/admin_add";
	private static final String GRANT_PAGE="admin/admin/grant";
	
	@RequestMapping(value = "/index")
	public String index(Map<String, Object> map,String page,HttpServletRequest request) {
		map.put("page", Admin.dao.paginate(toInt(page, 1), 15));
		return LIST_PAGE;
	}
	
	@RequestMapping(value = "/toAdd")
	public String toAdd(Map<String, Object> map,String id,HttpServletRequest request) {
		Admin w = Admin.dao.findById(id);
		map.put("bean", w);
		return ADD_PAGE;
	}
	
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete(Map<String, Object> map,String ids,HttpServletRequest request) {
		return Admin.dao.deleteByIds(ids)==true?ResponseKit.responseSuccess("操作成功!"):ResponseKit.responseError("操作失败！");
	}
	@RequestMapping(value = "/add")
	public @ResponseBody String add(Map<String, Object> map,String id,HttpServletRequest request) {
		List<Rule> rules=new ArrayList<>();
		rules.add(new Rule().setField("name").setLabel("昵称").setRules("required"));
		rules.add(new Rule().setField("loginName").setLabel("登录名称").setRules("required"));
		rules.add(new Rule().setField("password").setLabel("密码").setRules("required|callback_checkLoginNameExits"));
		String errorMsg = ValidForm.validform(request, rules);
		if(!StrKit.isBlank(errorMsg)){
			return ResponseKit.responseError(errorMsg);
		}
		boolean success=false;
		if(StrKit.isBlank(id)){
			Admin model = getModel(Admin.class);
			if(StrKit.isBlank(getPara("password"))){
				model.set("password", CryptUtils.parseStrToMd5L32("123456"));
			}else{
				model.set("password", CryptUtils.parseStrToMd5L32(getPara("password")));
			}
			success=model.set("date", new Date()).save();
		}else{
			Admin model=getModel(Admin.class);
			if(StrKit.isBlank(getPara("password"))){
				model.remove("password");
			}else{
				model.set("password", CryptUtils.parseStrToMd5L32(getPara("password")));
			}
			success=model.update();
		}
		return success==true?ResponseKit.responseSuccess("操作成功!"):ResponseKit.responseError("操作失败！");
	}
	
	@RequestMapping(value = "/toGrant")
	public String toGrant(Map<String, Object> map,String aid,HttpServletRequest request) {
		if(StrKit.isBlank(aid))return PAGE_404;
		map.put("aid", aid);
		int admin_role_id = Admin.dao.getRoleById(aid);
		map.put("rid", admin_role_id);
		List<Role> role_list = Role.dao.getAllRoles();
		map.put("role_list", role_list);
		return GRANT_PAGE;
	}
	
	@RequestMapping(value = "/grant")
	public @ResponseBody String grant(Map<String, Object> map,String aid,String rid,HttpServletRequest request) {
		if(StrKit.isBlank(aid)||StrKit.isBlank(rid)) return ResponseKit.responseError("丢失管理员ID和角色ID！请刷新，重试！");
		return Admin.dao.grant(rid,aid)?ResponseKit.responseSuccess("分配角色成功！"):ResponseKit.responseError("分配角色失败！");
	}
	
	@RequestMapping(value = "/checkLoginNameUni")
	public @ResponseBody String checkLoginNameUni(Map<String, Object> map,String loginName,HttpServletRequest request) {
		if(StrKit.isBlank(loginName)) return "{\"data\":{\"error\":\"不可以为空！\"}}";
		if (checkLoginNameExits(loginName )) {
			return "{\"data\":{\"error\":\"已经被注册！\"}}";
		}
		return "{\"data\":{\"ok\":\"可以注册！\"}}";
	}
	
	private boolean checkLoginNameExits(String loginName) {
		Admin a=Admin.dao.findByLoginName(loginName);
		return a==null? false : true;
	}
	
	
	
	
}
